/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sirtet.tetris;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import sirtet.Piece;
import sirtet.PieceProvider;

/**
 * A PieceProvider in which pieces come from a file
 * @author guangwei.zhu
 */
public class FilePieceProvider extends PieceProvider {

    protected LinkedList<TetrisPiece> pieces = new LinkedList<TetrisPiece>();
    private TetrisPiece dummy = new TetrisPieceFactory().fromString("1010001000101000000000000");
    // The above piece is an invalid piece only for padding.  It is X shape and hence not 4-connected

    public FilePieceProvider(File file) throws IOException {
        pieces.addAll(new TetrisPieceFactory().fromFile(file));
        pieces.addLast(dummy);
    }

    @Override
    public Piece requestPiece() {
        TetrisPiece next = pieces.pollFirst();
        if (next == null)
        {
            next = (TetrisPiece) dummy.clone();
        }
        return next;
    }

    public int remaining() {
        return pieces.size();
    }
}
